#include "httplib.h"
#include <iostream>
#include <string>
void Hello(const httplib::Request& req,httplib::Response& rsp)
{
    rsp.set_content("HELLO WORLD","text/plain");//设置正文
    rsp.status = 200;
}

void Numbers(const httplib::Request& req,httplib::Response& rsp)
{
    auto num = req.matches[1];//0里面保存的是整体path，往后的下标中保存的是捕捉到的数据
    rsp.set_content(num,"text/plain");
    rsp.status = 200;
}

void Multipart(const httplib::Request& req,httplib::Response& rsp)
{
    auto ret = req.has_file("file");//看看请求里有没有这个字段
    if(ret == false)
    {
        std::cout<<"not file upload\n";
        rsp.status = 404;
        return;
    }
    const auto& file = req.get_file_value("file");
    rsp.body.clear();
    rsp.body = file.filename;
    rsp.body += "\n";
    rsp.body += file.content;
    rsp.set_header("Content-Type","text/plain");
    rsp.status = 200;
    return;
}

int main()
{
    httplib::Server svr;//实例化一个服务器对象用于搭建服务器

    svr.Get("/hi",Hello);//注册一个针对/hi的Get请求的处理函数映射关系
    svr.Get(R"(/numbers/(\d+))",Numbers);
    svr.Post("/multipart",Multipart);
    svr.listen("0.0.0.0",8888);
    return 0;
}